package com.wang.net;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class Client {

	
	public static void main(String[] args) throws Exception{
		String host = "192.168.91.136";
		int port = 8088;
				
		Socket socket = new Socket(host, port);
		System.out.println("客户端启动了! "+String.format("连接ip %s,连接端口 %s",host,port));
		try(BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
				OutputStream out = socket.getOutputStream();
				InputStream in = socket.getInputStream()){
			
			String line;
			while((line=consoleReader.readLine())!=null){
				out.write(line.getBytes());
				out.flush();
				String data = read(in);
				System.out.println(data);
			}								
		
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			socket.close();
		}
	}
	
	private static final int BUFFER_SIZE = 1024;

    private static String read(InputStream in) throws Exception {
        byte[] buffer = new byte[BUFFER_SIZE];
        int count = in.read(buffer);
        return new String(buffer, 0, count);
    }
}
